package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.acl.service.impl.RoleServiceImpl;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户接口")
@RequestMapping("/admin/acl/user")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminRoleService adminRoleService;
    @Autowired
    private RoleService roleService;
    @ApiOperation("进行角色分配")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long adminId ,@RequestParam Long[] roleId){
        roleService.saveAdminRole(adminId,roleId);
        return Result.ok("");
    }
//   获取所有的角色,根据用户ID查询用户分配角色列表
    @ApiOperation("获取用户角色")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId){
//        返回map集合包含两部分数据,所有角色和为用户分配的角色列表
        Map<String,Object> adminRole = roleService.getRoleByAdminId(adminId);
        return Result.ok(adminRole);
    }
    //    角色列表
    //    用户列表方法
    @GetMapping("/{page}/{limit}")
    @ApiOperation("获取后台用户分页列表(带搜索)")
    public Result pageInfo(@PathVariable Long page, @PathVariable Long limit, AdminQueryVo adminQueryVo) {
        Page<Admin> pageList = new Page<>(page, limit);
        IPage<Admin> resultPage = adminService.selectByPage(pageList, adminQueryVo);
        return Result.ok(resultPage);
    }

    //    id查询用户
    @GetMapping("/get/{id}")
    @ApiOperation("根据ID查询数值")
    public Result getByID(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    //    添加用户
    @PostMapping("/save")
    @ApiOperation("添加用户")
    public Result addUser(@RequestBody Admin admin) {
//        获取输入的密码
        String password = admin.getPassword();
//        对密码进行加密
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
//        重新设置密码到实体
        admin.setPassword(md5Password);
        if (adminService.save(admin)) {
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }
    //    修改用户
    @PutMapping("/update")
    @ApiOperation("更新用户数据")
    public Result updateUser(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }

    //    删除用户
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除用户数据")
    public Result deleteUser(@PathVariable Long id) {
        if (adminService.removeById(id)) {
            Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    //    批量删除
    @DeleteMapping("/batchRemove")
    @ApiOperation("批量删除用户数据")
    public Result batchRemove(@RequestBody List<Long> ids) {
        if (adminService.removeByIds(ids)) {
            Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }
}
